home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / swing / DefaultButtonModel.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  12.1 KB  |  442 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)DefaultButtonModel.java    1.26 98/08/28
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package javax.swing;
  15.  
  16. import java.awt.*;
  17. import java.awt.event.*;
  18. import java.awt.image.*;
  19. import java.io.Serializable;
  20. import java.util.EventListener;
  21. import javax.swing.event.*;
  22.  
  23. /**
  24.  * The default implementation of a Button component's data model.
  25.  * <p>
  26.  * <strong>Warning:</strong>
  27.  * Serialized objects of this class will not be compatible with 
  28.  * future Swing releases.  The current serialization support is appropriate
  29.  * for short term storage or RMI between applications running the same
  30.  * version of Swing.  A future release of Swing will provide support for
  31.  * long term persistence.
  32.  *
  33.  * @version 1.26 08/28/98
  34.  * @author Jeff Dinkins
  35.  */
  36. public class DefaultButtonModel implements ButtonModel, Serializable {
  37.  
  38.     protected int stateMask = 0;
  39.     protected String actionCommand = null;
  40.     protected ButtonGroup group = null;
  41.         
  42.     protected int mnemonic = 0;
  43.  
  44.     /**
  45.      * Only one ChangeEvent is needed per button model instance since the
  46.      * event's only state is the source property.  The source of events
  47.      * generated is always "this".
  48.      */
  49.     protected transient ChangeEvent changeEvent = null;
  50.     protected EventListenerList listenerList = new EventListenerList();
  51.     
  52.         
  53.     /**
  54.      * Constructs a JButtonModel
  55.      *
  56.      */
  57.     public DefaultButtonModel() {
  58.         stateMask = 0;
  59.         setEnabled(true);
  60.     }
  61.         
  62.     /**
  63.      * Indicates partial commitment towards choosing the
  64.      * button.
  65.      */
  66.     public final static int ARMED = 1 << 0;
  67.         
  68.     /**
  69.      * Indicates that the button has been selected. Only needed for
  70.      * certain types of buttons - such as RadioButton or Checkbox.
  71.      */
  72.     public final static int SELECTED = 1 << 1;
  73.         
  74.     /**
  75.      * Indicates that the button has been "pressed"
  76.      * (typically, when the mouse is released).
  77.      */
  78.     public final static int PRESSED = 1 << 2;
  79.         
  80.     /**
  81.      * Indicates that the button can be selected by
  82.      * an input device (such as a mouse pointer).
  83.      */
  84.     public final static int ENABLED = 1 << 3;
  85.  
  86.     /**
  87.      * Indicates that the mouse is over the button.
  88.      */
  89.     public final static int ROLLOVER = 1 << 4;
  90.         
  91.     /**
  92.      * Sets the actionCommand string that gets sent as part of the
  93.      * event when the button is pressed.
  94.      *
  95.      * @param s the String that identifies the generated event
  96.      */
  97.     public void setActionCommand(String actionCommand) {
  98.         this.actionCommand = actionCommand;
  99.     }
  100.         
  101.     /**
  102.      * Returns the action command for this button. 
  103.      *
  104.      * @return the String that identifies the generated event
  105.      * @see #setActionCommand
  106.      */
  107.     public String getActionCommand() {
  108.         return actionCommand;
  109.     }
  110.  
  111.     /**
  112.      * Indicates partial commitment towards pressing the
  113.      * button. 
  114.      *
  115.      * @return true if the button is armed, and ready to be pressed
  116.      * @see #setArmed
  117.      */
  118.     public boolean isArmed() {
  119.         return (stateMask & ARMED) != 0;
  120.     }
  121.         
  122.     /**
  123.      * Indicates if the button has been selected. Only needed for
  124.      * certain types of buttons - such as RadioButton or Checkbox.
  125.      *
  126.      * @return true if the button is selected
  127.      */
  128.     public boolean isSelected() {
  129.         return (stateMask & SELECTED) != 0;
  130.     }
  131.         
  132.     /**
  133.      * Indicates if the button can be selected or pressed by
  134.      * an input device (such as a mouse pointer). (Checkbox-buttons
  135.      * are selected, regular buttons are "pressed".)
  136.      *
  137.      * @return true if the button is enabled, and therefore
  138.      *         selectable (or pressable)
  139.      */
  140.     public boolean isEnabled() {
  141.         return (stateMask & ENABLED) != 0;
  142.     }
  143.         
  144.     /**
  145.      * Indicates if button has been pressed.
  146.      *
  147.      * @return true if the button has been pressed
  148.      */
  149.     public boolean isPressed() {
  150.         return (stateMask & PRESSED) != 0;
  151.     }
  152.         
  153.     /**
  154.      * Indicates that the mouse is over the button.
  155.      *
  156.      * @return true if the mouse is over the button
  157.      */
  158.     public boolean isRollover() {
  159.         return (stateMask & ROLLOVER) != 0;
  160.     }
  161.         
  162.     /**
  163.      * Marks the button as "armed". If the mouse button is
  164.      * released while it is over this item, the button's action event
  165.      * fires. If the mouse button is released elsewhere, the
  166.      * event does not fire and the button is disarmed.
  167.      * 
  168.      * @param b true to arm the button so it can be selected
  169.      */
  170.     public void setArmed(boolean b) {
  171.         if((isArmed() == b) || !isEnabled()) {
  172.             return;
  173.         }
  174.             
  175.         if (b) {
  176.             stateMask |= ARMED;
  177.         } else {
  178.             stateMask &= ~ARMED;
  179.         }
  180.             
  181.         fireStateChanged();
  182.     }
  183.  
  184.     /**
  185.      * Enables or disables the button.
  186.      * 
  187.      * @param b true to enable the button
  188.      * @see #isEnabled
  189.      */
  190.     public void setEnabled(boolean b) {
  191.         if(isEnabled() == b) {
  192.             return;
  193.         }
  194.             
  195.         if (b) {
  196.             stateMask |= ENABLED;
  197.         } else {
  198.             stateMask &= ~ENABLED;
  199.         }
  200.             
  201.         fireStateChanged();
  202.     }
  203.         
  204.     /**
  205.      * Selects or deselects the button.
  206.      *
  207.      * @param b true selects the button,
  208.      *          false deselects the button.
  209.      */
  210.     public void setSelected(boolean b) {
  211.         if (this.isSelected() == b) {
  212.             return;
  213.         }
  214.  
  215.         if (b) {
  216.             stateMask |= SELECTED;
  217.         } else {
  218.             stateMask &= ~SELECTED;
  219.         }
  220.  
  221.         fireItemStateChanged(
  222.                 new ItemEvent(this,
  223.                               ItemEvent.ITEM_STATE_CHANGED,
  224.                               this,
  225.                               b ?  ItemEvent.SELECTED : ItemEvent.DESELECTED));
  226.         
  227.         fireStateChanged();
  228.         
  229.     }
  230.         
  231.         
  232.     /**
  233.      * Sets the button to pressed or unpressed.
  234.      * 
  235.      * @param b true to set the button to "pressed"
  236.      * @see #isPressed
  237.      */
  238.     public void setPressed(boolean b) {
  239.         if((isPressed() == b) || !isEnabled()) {
  240.             return;
  241.         }
  242.         
  243.         if (b) {
  244.             stateMask |= PRESSED;
  245.         } else {
  246.             stateMask &= ~PRESSED;
  247.         }
  248.  
  249.         if(!isPressed() && isArmed()) {
  250.             fireActionPerformed(
  251.                 new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
  252.                                 getActionCommand())
  253.                 );
  254.         }
  255.             
  256.         fireStateChanged();
  257.     }   
  258.  
  259.     /**
  260.      * Sets or clears the button's rollover state
  261.      * 
  262.      * @param b true to turn on rollover
  263.      * @see #isRollover
  264.      */
  265.     public void setRollover(boolean b) {
  266.         if((isRollover() == b) || !isEnabled()) {
  267.             return;
  268.         }
  269.         
  270.         if (b) {
  271.             stateMask |= ROLLOVER;
  272.         } else {
  273.             stateMask &= ~ROLLOVER;
  274.         }
  275.  
  276.         fireStateChanged();
  277.     }
  278.  
  279.     /**
  280.      * Sets the keyboard mnemonic (shortcut key or
  281.      * accelerator key) for this button.
  282.      *
  283.      * @param key an int specifying the accelerator key
  284.      */
  285.     public void setMnemonic(int key) {
  286.     mnemonic = key;
  287.     fireStateChanged();
  288.     }
  289.  
  290.     /**
  291.      * Gets the keyboard mnemonic for this model
  292.      *
  293.      * @return an int specifying the accelerator key
  294.      * @see #setMnemonic
  295.      */
  296.     public int getMnemonic() {
  297.     return mnemonic;
  298.     }
  299.  
  300.     /**
  301.      * Adds a ChangeListener to the button.
  302.      *
  303.      * @param l the listener to add
  304.      */
  305.     public void addChangeListener(ChangeListener l) {
  306.         listenerList.add(ChangeListener.class, l);
  307.     }
  308.     
  309.     /**
  310.      * Removes a ChangeListener from the button.
  311.      *
  312.      * @param l the listener to remove
  313.      */
  314.     public void removeChangeListener(ChangeListener l) {
  315.         listenerList.remove(ChangeListener.class, l);
  316.     }
  317.  
  318.     /*
  319.      * Notify all listeners that have registered interest for
  320.      * notification on this event type.  The event instance 
  321.      * is lazily created using the parameters passed into 
  322.      * the fire method.
  323.      *
  324.      * @see EventListenerList
  325.      */
  326.     protected void fireStateChanged() {
  327.         // Guaranteed to return a non-null array
  328.         Object[] listeners = listenerList.getListenerList();
  329.         // Process the listeners last to first, notifying
  330.         // those that are interested in this event
  331.         for (int i = listeners.length-2; i>=0; i-=2) {
  332.             if (listeners[i]==ChangeListener.class) {
  333.                 // Lazily create the event:
  334.                 if (changeEvent == null)
  335.                     changeEvent = new ChangeEvent(this);
  336.                 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
  337.             }          
  338.         }
  339.     }   
  340.     
  341.     /**
  342.      * Adds an ActionListener to the button.
  343.      *
  344.      * @param l the listener to add
  345.      */
  346.     public void addActionListener(ActionListener l) {
  347.         listenerList.add(ActionListener.class, l);
  348.     }
  349.         
  350.     /**
  351.      * Removes an ActionListener from the button.
  352.      *
  353.      * @param l the listener to remove
  354.      */
  355.     public void removeActionListener(ActionListener l) {
  356.         listenerList.remove(ActionListener.class, l);
  357.     }
  358.  
  359.     /*
  360.      * Notify all listeners that have registered interest for
  361.      * notification on this event type.  The event instance 
  362.      * is lazily created using the parameters passed into 
  363.      * the fire method.
  364.      *
  365.      * @param e the ActionEvent to deliver to listeners
  366.      * @see EventListenerList
  367.      */
  368.     protected void fireActionPerformed(ActionEvent e) {
  369.         // Guaranteed to return a non-null array
  370.         Object[] listeners = listenerList.getListenerList();
  371.         // Process the listeners last to first, notifying
  372.         // those that are interested in this event
  373.         for (int i = listeners.length-2; i>=0; i-=2) {
  374.             if (listeners[i]==ActionListener.class) {
  375.                 // Lazily create the event:
  376.                 // if (changeEvent == null)
  377.                 // changeEvent = new ChangeEvent(this);
  378.                 ((ActionListener)listeners[i+1]).actionPerformed(e);
  379.             }          
  380.         }
  381.     }   
  382.  
  383.     /**
  384.      * Adds an ItemListener to the button.
  385.      *
  386.      * @param l the listener to add
  387.      */
  388.     public void addItemListener(ItemListener l) {
  389.         listenerList.add(ItemListener.class, l);
  390.     }
  391.         
  392.     /**
  393.      * Removes an ItemListener from the button.
  394.      *
  395.      * @param l the listener to remove
  396.      */
  397.     public void removeItemListener(ItemListener l) {
  398.         listenerList.remove(ItemListener.class, l);
  399.     }
  400.  
  401.     /*
  402.      * Notify all listeners that have registered interest for
  403.      * notification on this event type.  The event instance 
  404.      * is lazily created using the parameters passed into 
  405.      * the fire method.
  406.      *
  407.      * @param e the ItemEvent to deliver to listeners
  408.      * @see EventListenerList
  409.      */
  410.     protected void fireItemStateChanged(ItemEvent e) {
  411.         // Guaranteed to return a non-null array
  412.         Object[] listeners = listenerList.getListenerList();
  413.         // Process the listeners last to first, notifying
  414.         // those that are interested in this event
  415.         for (int i = listeners.length-2; i>=0; i-=2) {
  416.             if (listeners[i]==ItemListener.class) {
  417.                 // Lazily create the event:
  418.                 // if (changeEvent == null)
  419.                 // changeEvent = new ChangeEvent(this);
  420.                 ((ItemListener)listeners[i+1]).itemStateChanged(e);
  421.             }          
  422.         }
  423.     }   
  424.  
  425.     /** Overriden to return null */
  426.     public Object[] getSelectedObjects() {
  427.         return null; 
  428.     }
  429.  
  430.     /**
  431.      * Identifies the group this button belongs to --
  432.      * needed for radio buttons, which are mutually
  433.      * exclusive within their group.
  434.      *
  435.      * @param group the ButtonGroup this button belongs to
  436.      */
  437.     public void setGroup(ButtonGroup group) {
  438.         this.group = group;
  439.     }
  440.  
  441. }
  442.